package utils

import java.io.File
import java.net.{InetAddress, NetworkInterface}

import application.mac.{Mac, NetInfo}
import javafx.scene.control.{Alert, ButtonType}
import javafx.scene.control.Alert.AlertType
import javafx.stage.Stage
import org.apache.commons.io.FileUtils
import tool.Tool

import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.concurrent.{CancellationException, Future, Promise}
import scala.concurrent.ExecutionContext.Implicits.global


/**
  * Created by yz on 2019/3/21
  */
object Utils {

  val isWindows = true

  def callScript(tmpDir: File, shBuffer: ArrayBuffer[String]) = {
    val execCommand = new ExecCommand
    val runFile = if (Utils.isWindows) {
      new File(tmpDir, "run.bat")
    } else {
      new File(tmpDir, "run.sh")
    }
    FileUtils.writeLines(runFile, shBuffer.asJava)
    val shCommand = runFile.getAbsolutePath
    if (Utils.isWindows) {
      execCommand.exec(shCommand, tmpDir)
    } else {
      val useCommand = "chmod +x " + runFile.getAbsolutePath
      val dos2Unix = "dos2unix " + runFile.getAbsolutePath
      execCommand.exec(dos2Unix, useCommand, shCommand, tmpDir)
    }
    execCommand

  }

  def file2Lines(file: File) = {
    FileUtils.readLines(file).asScala
  }

  def lines2File(file: File, lines: mutable.Buffer[String], append: Boolean = false) = {
    FileUtils.writeLines(file, lines.asJava, append)
  }

  def showInfo(header: String, msg: String): Unit = {
    val alert = new Alert(AlertType.INFORMATION)
    alert.setHeaderText(header)
    alert.setContentText(msg)
    alert.showAndWait
  }

  def showInformation(header: String, msg: String): Alert = {
    val alert = new Alert(AlertType.INFORMATION)
    alert.setHeaderText(header)
    alert.setContentText(msg)
    alert.getDialogPane.lookupButton(ButtonType.OK).setVisible(false)
    alert.show()
    alert
  }

  def showInformation(header: String, msg: String, stage: Stage): Alert = {
    val alert = new Alert(AlertType.INFORMATION)
    alert.setHeaderText(header)
    alert.setContentText(msg)
    alert.getDialogPane.lookupButton(ButtonType.OK).setVisible(false)
    alert.initOwner(stage)
    alert.show()
    alert
  }

  def getPrefix(file: File) = {
    val fileName = file.getName
    val index = fileName.lastIndexOf(".")
    fileName.substring(0, index)
  }

  def getLocalMac = {
    val ia = InetAddress.getLocalHost
    val mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress
    val sb = new StringBuilder
    for (i <- 0 until mac.length) {
      if (i != 0) sb.append("-")
      val temp = mac(i) & 0xff
      val str = Integer.toHexString(temp)
      if (str.length == 1) sb.append("0" + str) else sb.append(str)
    }
    sb.toString().toUpperCase
  }

  def getMac = {
    val mac = Mac.getMacAddress
    mac.replaceAll(":", "-")
  }

  def getMacs={
    val macs = new NetInfo().getMacs.asScala
    macs.map{mac=>
      mac.replaceAll(":", "-")
    }
  }

  def showError(header: String, msg: String): Unit = {
    val alert = new Alert(AlertType.ERROR)
    alert.setHeaderText(header)
    alert.setContentText(msg)
    alert.showAndWait
  }

  def showError(header: String, msg: String, stage: Stage): Unit = {
    val alert = new Alert(AlertType.ERROR)
    alert.setHeaderText(header)
    alert.setContentText(msg)
    alert.initOwner(stage)
    alert.showAndWait
  }


}
